<?php
ini_set('error_log', LOG_PATH.'error.log');
define('TP', DB_TABLE_PREFIX);
session_name(APP_ID.'_token');
$debug = '';



function xa_dump($args, $parsable = false) {
   $func = $parsable ? 'var_export' : 'print_r';
   return $func(count($args) > 1 ? $args : current($args), 1);
}



function e() {
   header('Content-Type: text/plain; charset=UTF-8');
   $e = new Exception("\n\n".xa_dump(func_get_args())."\n\n");
   die("\n".$e);
}



function x() {
   header('Content-Type: text/plain; charset=UTF-8');
   $e = new Exception("\n\n".xa_dump(func_get_args(), true)."\n\n");
   die("\n".$e);
}



function d() {
   $GLOBALS['debug'] .= ''.(new Exception("\n\n".xa_dump(func_get_args())."\n\n"));
}



function ew() {
   $e = new Exception("\r\n\r\n".iconv('UTF-8', 'IBM852', xa_dump(func_get_args()))."\r\n\r\n");
   die("\n".$e);
}


function xw() {
   $e = new Exception("\r\n\r\n".iconv('UTF-8', 'IBM852', xa_dump(func_get_args(), true))."\r\n\r\n");
   die("\n".$e);
}


function t() {
   $color = new XA_Console_Color();
   $e = new Exception(
		  "\n\n"
		. $color->getColoredString(xa_dump(func_get_args()), 'yellow')
		. "\n\n"
   );
   $file = fopen(LOG_PATH.'trace.log', 'a');
   fwrite($file, "\n".str_repeat('-', 76)."\n".$e);
   fclose($file);
}


function c() {
   $e = new Exception("\n\n".xa_dump(func_get_args())."\n\n");
   return "\n".$e;
}



function __autoload($class_name) {
   $file = 'lib/'.strtolower(substr($class_name, 3)).'.php';
   if (file_exists($filename = LIB_PATH.$file) || file_exists($filename = APP_PATH.$file)) {
      include($filename);
   }
   if (!class_exists($class_name, false)) {
      die(new Exception("\nERROR: $class_name not found\n"));
   }
}



function filter_keys($record, $key_list) {
   $return = array();

   foreach ($key_list as $key) {
      if (array_key_exists($key, $record)) {
         $return[$key] = $record[$key];
      }
   }

   return $return;
}



function qw($str) {
   $return = array();
   $str = str_replace("\n", ' ', $str);
   $str = str_replace("\t", ' ', $str);
   $str = str_replace("\r", ' ', $str);
   foreach (explode(' ', $str) as $item) {
      $item = trim($item);
      if (strlen($item)) {
         $return[] = $item;
      }
   }
   return $return;
}


class xa {

   protected static $object_list = array();
   public static $route_list = array();
   public static $model_list = array();
   public static $table_list = array();



   public static function perm() {
      return true;
      if (empty(self::$object_list['perm'])) {
         self::$object_list['perm'] = new XA_Permission();
      }
      return self::$object_list['perm']->check(func_get_args());
   }



   public static function query($sql) {
      if (empty(self::$object_list['db'])) {
         self::$object_list['db'] = XA_Db::getInstance();
      }
      return self::$object_list['db']->query($sql);
   }



   public static function table($table_name) {
      if (empty(self::$table_list[$table_name])) {
         if (defined('DB_TYPE') && DB_TYPE === 'pgsql') {
            self::$table_list[$table_name] = new XA_Db_Table_PgSQL($table_name);
         } elseif (defined('DB_TYPE') && DB_TYPE === 'sybase') {
            self::$table_list[$table_name] = new XA_Db_Table_Sybase($table_name);
         } else {
            self::$table_list[$table_name] = new XA_Db_Table_MySQL($table_name);
         }
      }
      return self::$table_list[$table_name];
   }



   public static function say($text) {
      return $text;
   }



   public static function log($params = array(), $log_list = 'app.log') {
      XA_Log::log($params, $log_list);
   }



   public static function url($param_list = array(), $for_html = true) {
      if (is_string($param_list)) {
         parse_str($param_list, $param_list);
      }

      if (!empty($param_list['action'])) {
         $action = $param_list['action'];
      } else {
         return defined('MODULE_URL') ? MODULE_URL : SITE_URL;
      }

      if (!isset(self::$route_list[$action])) {
         if (file_exists(MODULE_PATH.'routes/'.$action.'.php')) {
            include(MODULE_PATH.'routes/'.$action.'.php');
         } else {
            self::$route_list[$action] = false;
         }
      }

      if ($gen = self::$route_list[$action]) {
         return $gen($param_list, $for_html);
      } else {
         return (defined('MODULE_URL') ? MODULE_URL : SITE_URL).($param_list ? '?'.http_build_query($param_list, '', ($for_html ? '&amp;' : '&')) : '');
      }
   }
}
